import 'package:bottom_sheet/bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mode/utils.dart';
import 'package:rflutter_alert/rflutter_alert.dart';

class DialogDemo extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DialogDemoState();
  }
}

class _DialogDemoState extends State<DialogDemo> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('Dialog and BottomSheet'),
          centerTitle: true,
        ),
        body: SingleChildScrollView(
          child: Wrap(
            spacing: 8,
            runSpacing: 4,
            children: [
              addTitle('rflutter_alert'),
              addAction(
                  '简单',
                  () => Alert(
                          context: context,
                          title: 'Alert Demo',
                          desc: '“你好”是对别人的一种尊敬，遇到认识的人或陌生的人都可以说的。')
                      .show()),
              addAction(
                  '复杂',
                  () => Alert(
                          context: context,
                          title: 'Alert Demo',
                          desc: '“你好”是对别人的一种尊敬，遇到认识的人或陌生的人都可以说的。',
                          image: Image.asset('assets/img/girl_header.jpg'),
                          content: Text(
                            '这是自定义部分，可以根据需求布局。',
                            style: TextStyle(color: Colors.amberAccent),
                          ),
                          style: AlertStyle(
                              animationType: AnimationType.fromRight),
                          buttons: [
                            DialogButton(
                              child: Text(
                                "OK",
                                style: TextStyle(
                                    color: Colors.white, fontSize: 20),
                              ),
                              onPressed: () => Navigator.pop(context),
                              color: Color.fromRGBO(0, 179, 134, 1.0),
                            ),
                            DialogButton(
                              child: Text(
                                "继续",
                                style: TextStyle(
                                    color: Colors.white, fontSize: 20),
                              ),
                              onPressed: () => Navigator.pop(context),
                              gradient: LinearGradient(colors: [
                                Color.fromRGBO(116, 116, 191, 1.0),
                                Color.fromRGBO(52, 138, 199, 1.0)
                              ]),
                            )
                          ]).show()),
              addTitle('bottom_sheet'),
              addAction('BottomSheet', _showBottomSheet),
              addAction('StickyBottomSheet', _showStickyFlexibleBottomSheet),
              addTitle('其他'),
              addAction('自定义BottomSheet',
                  () => showMyBottomSheet(context, ['name', 'age', 'school'])),
              addAction(
                  '自定义Alert',
                  () => showAlert(
                      context, '建设良好网络生态，必须依法打击网络谣言。建设良好网络生态，必须依法打击网络谣言。')),
              addAction('Toast',
                  () => showToast('建设良好网络生态，必须依法打击网络谣言。建设良好网络生态，必须依法打击网络谣言。'))
            ],
          ),
        ));
  }

  addTitle(String title) {
    return Center(
      child: Text(title),
    );
  }

  addAction(String title, Function() action) {
    return ElevatedButton(
      onPressed: action,
      child: Text(title),
    );
  }

  _showBottomSheet() {
    showFlexibleBottomSheet(
      minHeight: 0,
      initHeight: 0.5,
      maxHeight: 1,
      context: context,
      builder: (context, scrollController, bottomSheetOffset) {
        return SafeArea(
          bottom: false,
          child: Material(
            child: Container(
              child: ListView.builder(
                controller: scrollController,
                itemBuilder: (context, position) => Padding(
                  padding: EdgeInsets.all(30),
                  child: Text('$position'),
                ),
                itemCount: 41,
              ),
            ),
          ),
        );
      },
      anchors: [0, 0.5, 1],
    );
  }
  _showStickyFlexibleBottomSheet() {
    showStickyFlexibleBottomSheet(
      minHeight: 0,
      initHeight: 0.5,
      maxHeight: 1,
      headerHeight: 200,
      context: context,
      headerBuilder: (BuildContext context, double offset) {
        return AnimatedContainer(
          duration: const Duration(milliseconds: 300),
          width: double.infinity,
          height: 200,
          decoration: BoxDecoration(
            color: Colors.green,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(offset == 0.8 ? 0 : 40),
              topRight: Radius.circular(offset == 0.8 ? 0 : 40),
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Expanded(
                child: Center(
                  child: Text(
                    'Header',
                    style: Theme.of(context).textTheme.headlineMedium,
                  ),
                ),
              ),
              Text(
                'position $offset',
                style: Theme.of(context).textTheme.titleLarge,
              ),
            ],
          ),
        );
      },
      bodyBuilder: (BuildContext context, double offset) {
        return SliverChildListDelegate(
          <Widget>[
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
            Container(padding: EdgeInsets.all(30), color: Colors.white, child: Text('xxx'),),
          ],
        );
      },
      anchors: [0, 0.5, 1],
    );
  }
}
